<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PostgreSQL Connection Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#wrapper {
  width: 600px;
  margin: 20px auto 0;
  font: 1.2em Verdana, Arial, sans-serif;
}
input {
  font-size: 1em;
}
#submit {
  padding: 4px 8px;
}
</style>
</head>

<body>

<div id="wrapper">

<?php
$action = htmlspecialchars($_GET['action'], ENT_QUOTES);
?>

<?php if (!$action) { ?>

  <h1>PostgreSQL connection test</h1>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=test" id="mail" method="post">

  <table cellpadding="2">
    <tr>
      <td></td>
      <td><input type="submit" id="submit" value="Test Connection" tabindex="5" /></td>
      <td></td>
    </tr>
  </table>

</form>

<?php } ?>

<?php if ($action == "test") {
ini_set('display_errors', 1);
error_reporting(E_ALL);
// The variables have not been adequately sanitized to protect against SQL Injection attacks: http://us3.php.net/mysql_real_escape_string

require_once "class.morphini.php";

$Morph = new MorphIni();
$dbhost = $Morph->db_hostname;
$dbport = $Morph->db_port;
$dbtype = $Morph->db_type;
$dbname = $Morph->db_name;
$dbuser = $Morph->db_username;
$dbpass = $Morph->db_password;

$dbconn = "host=$dbhost port=$dbport dbname=$dbname user=$dbuser password=$dbpass";
print "<pre>";

$link = pg_connect($dbconn);
    if (!$link) {
      echo "<p><b>Could not connect to server:</b> '" . $hostname . "'</p>\n";
        echo pg_last_error();
    }else{
      echo "<p><b>Successfully connected to server:</b> '" . $dbhost . "'</p>\n";
      printf("<b>PostgreSQL client encoding info:</b> %s</br>", pg_client_encoding());
      printf("<b>PostgreSQL host info:</b> %s</br>", pg_host());
      $pg_ver=pg_version();
      printf("<b>PostgreSQL Client version:</b> %s</br>", $pg_ver["client"]);
      printf("<b>PostgreSQL Server version:</b> %s</br>", $pg_ver["server"]);
      printf("<b>PostgreSQL Server Protocol version:</b> %s</br>", $pg_ver["protocol"]);
    }
  if ($link && !$dbname) {
    echo "<p><b>No database name was given. Available databases:</b></p>\n";
    echo "<pre>\n";
    echo "</pre>\n";
  }
  if ($dbname) {
      echo "<p><b>Successfully connected to database:</b> '" . $dbname . "'</p>\n";
      // Check tables
      $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';";
      $result = pg_query($sql);
      if (pg_num_rows($result) > 0) {
        echo "<p><b/>Available tables:</b></p>\n";
        echo "<pre>\n";
        $ct=0;
        while ($row = pg_fetch_row($result)) {
          echo "{$row[$ct]}\n";
          $asql="SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$row[$ct]';";
          $ct=$ct+1;
          $aresult = pg_query($asql);
          if (pg_num_rows($aresult) > 0) {
            echo "<p><b/>Table description:</b></p>\n";
            echo "<pre>\n";
            $act=0;
            while ($arow = pg_fetch_row($aresult)) {
              echo "{$arow[0]}\n";
              $act=$act+1;
            }
          }
        }
        echo "</pre>\n";
        echo "</pre>\n";
        $t=date('y/m/d h:i:s');
        $sql = "INSERT INTO t1 VALUES(DEFAULT, '" . $t . "');";
        echo "<p><b>Testing insert:</b> ".$sql."</p>\n";
        $result = pg_query($sql);
        $sql="SELECT * FROM t1 ORDER BY id DESC LIMIT 1;";
        echo "</p><b>Testing Select:</b> ".$sql."</p>";
        echo "<p><b>Data:</b></p>";
        echo "<pre>\n";
        $result = pg_query($sql);
        while ($row = pg_fetch_row($result)) {
          echo "{$row[0]}, $row[1]\n";
        }
        echo "</pre>\n";
      } else {
        echo "<p><b>The database '" . $dbname . "' contains no tables.</b></p>\n";
        $sql= "CREATE SEQUENCE serial START 1 INCREMENT 1; CREATE TABLE t1 (id integer NOT NULL DEFAULT nextval('serial'), name CHAR(30) NOT NULL);";
        echo "<p><b>Creating table:</b> ".$sql."</p>";
        $result = pg_query($sql);
        echo $result;
        echo pg_last_error();
      }
?>
</br></br></br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=test" id="mail" method="post">

  <table cellpadding="2">
    <tr>
      <td></td>
      <td><input type="submit" id="submit" value="ReTest Connection" tabindex="5" /></td>
      <td></td>
    </tr>
  </table>

</form>
</br></br></br>

<?
  }
   pg_close($link);
}
?>

</div><!-- end #wrapper -->
<?php phpinfo(); ?>
</body>
</html>
